package com.git.smp.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.git.smp.core.PageRequest;
import com.git.smp.core.ResponseMessage;
import com.git.smp.dao.RequireApplyMapper;
import com.git.smp.dao.RequirementInfoMapper;
import com.git.smp.entity.QmsRequest;
import com.git.smp.entity.QmsResponse;
import com.git.smp.entity.QmsResponseBody;
import com.git.smp.entity.RequireApply;
import com.git.smp.entity.RequirementInfo;
import com.git.smp.entity.RequirementInfoExtend;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/requirementInfo")
public class RequirementInfoController {
	
	Logger logger = LoggerFactory.getLogger(RequirementInfoController.class);
	
	@Autowired
	RequirementInfoMapper requirementInfoMapper;
	
	@Autowired
	RequireApplyMapper requireApplyMapper;
	
	@Autowired
	RestTemplate reseRestTemplate;
	
	@Value("${qmsUrl}")
	private String qmsUrl;
	
	/**
	 * 保存需求信息
	 */
	@RequestMapping("/saveRequirementInfo")
	public ResponseMessage<?>  saveRequirementInfo(@RequestBody RequirementInfo requirementInfo){
		ResponseMessage<?> responseMessage = new ResponseMessage();
		responseMessage.setCode("0000");
		responseMessage.setMessage("保持成功！");
		if(requirementInfoMapper.selectByPrimaryKey(requirementInfo.getQmsNo())==null){
			requirementInfoMapper.insert(requirementInfo);
		}
		
		return responseMessage;
	}
	
	
	//分页查询需求信息列表
	@RequestMapping("/getRequirementInfoListByPage")
	public ResponseMessage<PageInfo<RequirementInfo>> getRequirementInfoListByPage(@RequestBody PageRequest<RequirementInfoExtend> pageRequest) {
		ResponseMessage<PageInfo<RequirementInfo>> responseMessage = new ResponseMessage<PageInfo<RequirementInfo>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询需求信息列表成功！");

		RequirementInfoExtend requirementInfoExtend = pageRequest.getBody();

		PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
		List<RequirementInfo> listRequirementInfo = requirementInfoMapper.getRequirementInfoListByPage(requirementInfoExtend);
		if (listRequirementInfo == null) {
			responseMessage.setCode("0001");
			responseMessage.setMessage("查询需求信息列表失败！");
		}
		PageInfo<RequirementInfo> pageInfo = new PageInfo<RequirementInfo>(listRequirementInfo, pageRequest.getPageSize());
		responseMessage.setExtend(pageInfo);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	
	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/getRequirementInfoList")
	public ResponseMessage<List<RequirementInfo>> getRequirementInfoList(@RequestParam String query) {
		ResponseMessage<List<RequirementInfo>> responseMessage = new ResponseMessage<List<RequirementInfo>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询需求信息列表成功！");


		PageHelper.startPage(1, 5);
		List<RequirementInfo> listRequirementInfo = requirementInfoMapper.getRequirementInfoList(query);
		if (listRequirementInfo == null ||listRequirementInfo.size()<=0) {
			//查询qms数据信息
			logger.info("查询qms信息");
			QmsRequest request = new QmsRequest();
			request.setQmsNo(query);
			ResponseEntity<QmsResponse> response = reseRestTemplate.postForEntity(qmsUrl, request, QmsResponse.class);
			QmsResponse qmsResponse = response.getBody();
			List<QmsResponseBody> qmsResponseBodyList = qmsResponse.getBody();
			if(qmsResponseBodyList!=null) {
				 listRequirementInfo = new ArrayList<RequirementInfo>();
				for(QmsResponseBody qmsResponseBody:qmsResponseBodyList) {
					RequirementInfo requirementInfo = new RequirementInfo();
					requirementInfo.setQmsNo(qmsResponseBody.getQmsNo());
					requirementInfo.setQmsName(qmsResponseBody.getQmsName());
					requirementInfo.setMainSystemNo(qmsResponseBody.getSystemNo());
					listRequirementInfo.add(requirementInfo);
					
					requirementInfoMapper.insert(requirementInfo);
					
					
				}
			}
		}
		
		responseMessage.setExtend(listRequirementInfo);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	
	//查询需求信息
	@RequestMapping("getRequirementInfo")
	public ResponseMessage<RequirementInfo> getRequirementInfo(@RequestParam String qmsNo){
		
		ResponseMessage<RequirementInfo> responseMessage = new ResponseMessage<RequirementInfo>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询需求信息成功！");
		
		RequirementInfo requirementInfo = requirementInfoMapper.getRequirementInfo(qmsNo);
		
		responseMessage.setExtend(requirementInfo);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	/*
	 * //根据需求号分页查询对应工单信息
	 * 
	 * @RequestMapping("getWorkOrderInfoByQmsNo") public
	 * ResponseMessage<PageInfo<RequireApply>> getWorkOrderInfoByQmsNo(
	 * 
	 * @RequestBody PageRequest<RequirementInfo> pageRequest ){
	 * ResponseMessage<PageInfo<RequireApply>> responseMessage = new
	 * ResponseMessage<PageInfo<RequireApply>>(); responseMessage.setCode("0000");
	 * responseMessage.setMessage("根据需求号分页查询对应工单信息成功！");
	 * 
	 * PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
	 * List<RequireApply> listRequireApply =
	 * requireApplyMapper.getWorkOrderInfoByQmsNo(pageRequest.getBody().getQmsNo());
	 * 
	 * if (listRequireApply == null) { responseMessage.setCode("0001");
	 * responseMessage.setMessage("根据需求号分页查询对应工单信息失败！"); } PageInfo<RequireApply>
	 * pageInfo = new PageInfo<RequireApply>(listRequireApply,
	 * pageRequest.getPageSize()); responseMessage.setExtend(pageInfo);
	 * logger.info(responseMessage.getMessage()); return responseMessage;
	 * 
	 * }
	 */

}
